package com.example.xiaomaotv.adapter;

import static android.app.PendingIntent.getActivity;
import static androidx.core.content.ContextCompat.startActivity;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.xiaomaotv.MainActivity2;
import com.example.xiaomaotv.R;
import com.example.xiaomaotv.SyFragment;
import com.example.xiaomaotv.entity.TvList;

import java.util.List;

public class RecycleTVAdapter extends RecyclerView.Adapter<RecycleTVAdapter.TvViewHolder> {
    private List<TvList> tvData;

    public RecycleTVAdapter(List<TvList> tvData) {
        this.tvData = tvData;
    }
    public static class TvViewHolder extends RecyclerView.ViewHolder{
        private ImageView tvImg;
        private TextView tvName;
        private TextView tvBrief;
        private TextView tvTime;
        private View tvView;

        public TvViewHolder(@NonNull View itemView) {
            super(itemView);
            this.tvImg = itemView.findViewById(R.id.tv_img);
            this.tvName = itemView.findViewById(R.id.tv_name);
            this.tvBrief = itemView.findViewById(R.id.tv_brief);
            this.tvTime = itemView.findViewById(R.id.tv_time);
            this.tvView =itemView;
        }
    }


    @NonNull
    @Override
    public TvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View tvView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sylist,null);
        TvViewHolder holder = new TvViewHolder(tvView);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull TvViewHolder holder, int position) {
        TvList tvList = tvData.get(position);
        holder.tvImg.setImageResource(tvList.getImg());
        holder.tvName.setText(tvList.getName());
        holder.tvBrief.setText(tvList.getBrief());
        holder.tvTime.setText(tvList.getTime());
        holder.tvView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                TvList selectedItem = tvData.get(holder.getAdapterPosition());
                Intent intent = new Intent(context,MainActivity2.class);
                // 传递数据
                intent.putExtra("Name", selectedItem.getName());
                intent.putExtra("year", selectedItem.getTime());
                intent.putExtra("Img", selectedItem.getImg());
                intent.putExtra("Brief", selectedItem.getBrief());
                // 启动Activity
                context.startActivity(intent);
            }
        });
    }
    @Override
    public int getItemCount() {
        return tvData.size();
    }
}
